package com.fivefu.core.hillstone.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fivefu.base.common.utils.str.StrUtils;
import com.fivefu.core.hillstone.entity.DbHillstoneOrganizationDevice;
import com.fivefu.core.hillstone.mapper.DbHillstoneOrganizationDeviceMapper;
import com.fivefu.core.hillstone.services.DbHillstoneOrganizationDeviceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 大华_组织设备关联表 服务实现类
 * </p>
 *
 * @author 
 * @since 2022-08-23 15:34:10
 */
@Service
public class DbHillstoneOrganizationDeviceServiceImpl extends ServiceImpl<DbHillstoneOrganizationDeviceMapper, DbHillstoneOrganizationDevice> implements DbHillstoneOrganizationDeviceService {

    /**
     * 通过设备编码查询组织设备信息
     * @param orgCode      组织编码
     * @param deviceId     设备编码
     * @return
     */
    public List<DbHillstoneOrganizationDevice> queryOrganizationDevice(String orgCode, String deviceId) {
        QueryWrapper<DbHillstoneOrganizationDevice> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        wrapper.eq("org_code",orgCode);//组织编码
        wrapper.eq("device_code",deviceId);//设备编码
        List<DbHillstoneOrganizationDevice> organizationDeviceList = baseMapper.selectList(wrapper);
        return organizationDeviceList;
    }

    /**
     * 删除多余组织设备关联数据信息
     * @param orgCode      组织编码
     * @param deviceId     设备编码
     */
    @Override
    public void deleteRedundantData(String orgCode,String deviceId) {
        LocalDateTime time = LocalDateTime.now();
        List<DbHillstoneOrganizationDevice> oldOrganizationDeviceList = queryOrganizationDevice(orgCode,deviceId);

        //判断是否已经存在相关数据信息
        if (null != oldOrganizationDeviceList && oldOrganizationDeviceList.size() >0 ) {
            List<Long> deviceIds = oldOrganizationDeviceList.stream().map(k -> k.getId()).collect(Collectors.toList());
            Map<String, Long> deviceMap = oldOrganizationDeviceList.stream().collect(Collectors.toMap(k -> k.getDeviceCode(), v -> v.getId()));
            if (StrUtils.isNotNull(deviceId)) {
                if (deviceMap.containsKey(deviceId)) {
                    Long id = deviceMap.get(deviceId);
                    deviceIds.remove(id);
                }
            }

            //删除多余组织设备数据
            if (deviceIds.size() > 0) {
                DbHillstoneOrganizationDevice device = new DbHillstoneOrganizationDevice();
                device.setIsDelete(1);
                device.setUpdatedBy("sys");
                device.setUpdatedTime(time);

                QueryWrapper<DbHillstoneOrganizationDevice> wrapper = new QueryWrapper<>();
                wrapper.in("id",deviceIds);
                wrapper.eq("org_code",orgCode);
                wrapper.eq("device_code",deviceId);//设备编码
                wrapper.eq("is_delete",0);
                baseMapper.update(device,wrapper);
            }
        }
    }

    /**
     * 编辑组织设备通道信息
     * @param orgCode      组织编码
     * @param channelId    通道编码
     * @param deviceId     设备编码
     */
    @Override
    public void editDbHillstoneOrganizationChannel(String orgCode,String channelId,String deviceId) {
        LocalDateTime time = LocalDateTime.now();
        DbHillstoneOrganizationDevice organizationDevice = new DbHillstoneOrganizationDevice();
        organizationDevice.setChannelCode(channelId);//通道编码
        organizationDevice.setOrgCode(orgCode);
        organizationDevice.setDeviceCode(deviceId);//设备编码
        DbHillstoneOrganizationDevice oldOrganizationDevice = queryHillstoneOrganizationDevice(orgCode, deviceId);
        organizationDevice.setUpdatedBy("sys");
        organizationDevice.setUpdatedTime(time);

        if (null != oldOrganizationDevice) {
            organizationDevice.setId(oldOrganizationDevice.getId());
            baseMapper.updateById(organizationDevice);
        } else {
            organizationDevice.setCreatedBy("sys");
            organizationDevice.setCreatedTime(time);
            organizationDevice.setIsDelete(0);
            baseMapper.insert(organizationDevice);
        }
    }

    /**
     * 编辑组织设备关联表
     * @param orgCode      组织编码
     * @param deviceId     设备编码
     */
    @Override
    public void editDbHillstoneOrganizationDevice(String orgCode, String deviceId) {
        DbHillstoneOrganizationDevice organizationDevice = new DbHillstoneOrganizationDevice();
        organizationDevice.setOrgCode(orgCode);
        organizationDevice.setDeviceCode(deviceId);

        LocalDateTime time = LocalDateTime.now();
        DbHillstoneOrganizationDevice oldOrganizationDevice = queryHillstoneOrganizationDevice(orgCode, deviceId);
        organizationDevice.setUpdatedTime(time);
        organizationDevice.setUpdatedBy("sys");

        if (null != oldOrganizationDevice) {
            organizationDevice.setId(oldOrganizationDevice.getId());
            baseMapper.updateById(organizationDevice);
        } else {
            organizationDevice.setCreatedBy("sys");
            organizationDevice.setCreatedTime(time);
            organizationDevice.setIsDelete(0);
            baseMapper.insert(organizationDevice);
        }
    }

    /**
     * 通过组织编码和设备编码查询组织设备关联关系
     * @param orgCode    组织编码
     * @param deviceId   设备编码
     * @return
     */
    @Override
    public DbHillstoneOrganizationDevice queryHillstoneOrganizationDevice(String orgCode, String deviceId) {
        QueryWrapper<DbHillstoneOrganizationDevice> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        wrapper.eq("org_code",orgCode);//组织编码
        wrapper.eq("device_code",deviceId);//设备编码
        DbHillstoneOrganizationDevice organizationDevice = baseMapper.selectOne(wrapper);
        return organizationDevice;
    }
}
